package com.ttai.presenter.activity;

import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ttai.model.dao.bean.Id_Item;
import com.ttai.model.net.IdAcountBean;
import com.ttai.model.net.ResponseInfo;
import com.ttai.presenter.base.BasePresenter;
import com.ttai.ui.activity.AddIdAcount;
import com.ttai.ui.activity.ChooseId;

import java.util.ArrayList;

import retrofit2.Call;

public class QrListPresenter extends BasePresenter {
    ChooseId chooseId;
    ArrayList<Id_Item> idList = new ArrayList<>();

    public QrListPresenter(ChooseId chooseId) {
        this.chooseId = chooseId;
    }

    public ArrayList<Id_Item> getIdList() {
        return idList;
    }

    @Override
    protected void showError(String message) {
        Toast.makeText(chooseId,message,Toast.LENGTH_SHORT).show();

    }

    @Override
    protected void parseJson(JsonObject json) {
        Gson gson = new Gson();
        IdAcountBean idAcountBean = gson.fromJson(json,IdAcountBean.class);
        ArrayList<IdAcountBean.AccountBean> idAcountList = (ArrayList<IdAcountBean.AccountBean>) idAcountBean.getAccount();
        for (int i = 0; i < idAcountList.size(); i++) {
            String nameNumber = idAcountList.get(i).getAppName();
            String img = idAcountList.get(i).getIcon();
            String appid = String.valueOf(idAcountList.get(i).getAppid());
            String mynumber = idAcountList.get(i).getUseraccount();
            String logo = idAcountList.get(i).getAccountflag();

            if (chooseId.getRealAppId().equals(appid)){
                Id_Item id_item = new Id_Item(img,nameNumber,mynumber,logo);
                idList.add(id_item);
            }
        }
        chooseId.getLoginIdRecycleViewAdapter().setData(idList);
    }

    public void storeIdAcount(String token){
        Call<ResponseInfo> idTokenInfo = responseInfoApi.restoreIdAcount(token);
        idTokenInfo.enqueue(new CallBackAdapter());
    }
}
